package com.social.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.social.constant.RedisConstants;
import com.social.dao.MenuDao;
import com.social.pojo.Menu;
import com.social.pojo.Result;
import com.social.service.MenuService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static com.social.constant.MessageConstant.*;
import static com.social.constant.RedisConstants.SYSTEM_MENU_KEY;
import static com.social.constant.RedisConstants.SYSTEM_MENU_TTL;


/**
 * @className: MenuServiceImpl
 * @description: 系统字典
 * @author: XiaoXu
 * @date: 2022/10/30 16:21
 **/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuDao, Menu> implements MenuService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result getMenuByPath(String path) {
        String key = SYSTEM_MENU_KEY + path;
        String json = stringRedisTemplate.opsForValue().get(key);
        if(json != null){
            List<Menu> menus = JSONUtil.toList(json, Menu.class);
            stringRedisTemplate.expire(key,SYSTEM_MENU_TTL, TimeUnit.MINUTES);
            return Result.ok(OPERATE_DATA_SUCCESS,menus);
        }
        List<Menu> menus = query().eq("path",path).ne("father",-1).list();
        List<Menu> menuList = menus.stream().filter(its -> its.getFather() == 0).collect(Collectors.toList());
        for (Menu menu : menuList) {
            if (menu.getFather() == 0){
                menu.setSons(getSons(menu.getId(), menus));

            }
        }
        stringRedisTemplate.opsForValue().set(key,JSONUtil.toJsonStr(menuList),SYSTEM_MENU_TTL, TimeUnit.MINUTES);
        return Result.ok(OPERATE_DATA_SUCCESS,menuList);
    }

    @Override
    public Result insert(Menu menu) {
        boolean isSuccess = save(menu);

        if(!isSuccess){
            return Result.fail(SAVA_DATA_FAIL,"数据添加失败");
        }
        String key = SYSTEM_MENU_KEY + menu.getPath();
        stringRedisTemplate.delete(key);
        return Result.ok(OPERATE_DATA_SUCCESS);
    }

    @Override
    public Result updateMenu(Menu menu) {

        String path = getById(menu.getId()).getPath();
        String key = SYSTEM_MENU_KEY + path;

        boolean isSuccess = updateById(menu);
        if(!isSuccess){
            return Result.fail(UPDATE_DATA_FAIL,"修改失败");
        }

        Boolean delete = stringRedisTemplate.delete(key);

        if(!delete){
            return Result.fail(UPDATE_DATA_FAIL,"修改失败");
        }

        return Result.ok(OPERATE_DATA_SUCCESS);
    }

    @Override
    public Result deleteMenu(Menu menu) {
        menu.setFather(-1L);
        return updateMenu(menu);
    }

    private List<Menu> getSons(Long id, List<Menu> menus) {
        List<Menu> sons = new ArrayList<>();
        for (Menu menu : menus) {
            if(menu.getFather().equals(id)){
                menu.setSons(getSons(menu.getId(),menus));
                sons.add(menu);
            }
        }
        return sons;
    }


}
